<?php

class DB_Functions {

    private $db;

    //put your code here
    // constructor
    function __construct() {
        require_once 'DB_Connect.php';
        // connecting to database
        $this->db = new DB_Connect();
        $this->db->connect();
    }

    // destructor
    function __destruct() {

    }


    public function getUser($username, $password) {
        $md5Pass = md5($password);
        $result = mysql_query("SELECT * FROM care_users WHERE login_id = '$username' AND password = '$md5Pass' ")
           or die(mysql_error());
        $numRows = mysql_num_rows($result);
        if ($numRows > 0) { //USER FOUND
            $result = mysql_fetch_array($result);
            return $result;
        } else { //USER NOT FOUND
            return false;
        }
    }

    public function getPatientData ($pid) {
        $query = "SELECT pid, date_reg, name_first, name_last, nat_id_nr, ethnic_orig, " .
            "date_birth, sex, blood_group, birth_country, birth_entity, birth_town, birth_city, " .
            "civil_status, illiterate, educational_level, educational_plan, years_approved, profession, " .
            "sso, addr_entity, addr_citytown_name, addr_town, addr_locality, addr_sector, addr_str, addr_house, " .
            "addr_floor, addr_reference, addr_time, name_mother, name_father, create_id " .
            "FROM care_person WHERE pid='" . $pid . "'";
        $result = mysql_query($query) or die(mysql_error());
        $result = mysql_fetch_array($result);
        return $result; //ToDo: need to check for errors
    }

    public function getPatientFamBack ($pid) {
        $query = "SELECT DISTINCT id_correlativo
          FROM  `care_historia_datos_historia_clinica` 
          WHERE id_paciente =".$pid."
            AND ficha =  'antecedentes'
            AND  `id_campo` =  'cancer'";
        $result = mysql_query($query) or die(mysql_error());
        $result = mysql_fetch_array($result);
        $index = $result[0];
        
        $query = "SELECT id_campo, valor
          FROM  `care_historia_datos_historia_clinica` 
          WHERE id_correlativo =".$index;
        $result = mysql_query($query) or die(mysql_error());
        $lista = array();
        while ($row = mysql_fetch_assoc($result)) {
            $lista[] = $row["id_campo"];
            $lista[] = $row["valor"];
        }
        
        return $lista; //ToDo: return errors
    }

    public function getPatientPerBack ($pid) {
        $query = "SELECT DISTINCT id_correlativo
          FROM  `care_historia_datos_historia_clinica`
          WHERE id_paciente =".$pid."
            AND (
              id_campo =  'telarquia' OR
              id_campo =  'pubarquia'
            )";
        $result = mysql_query($query) or die(mysql_error());
        $result = mysql_fetch_array($result);
        $index = $result[0];

        $query = "SELECT id_campo, valor
          FROM  `care_historia_datos_historia_clinica`
          WHERE id_correlativo =".$index."
          AND ficha='antecedentes'";
        $result = mysql_query($query) or die(mysql_error());
        $lista = array();
        while ($row = mysql_fetch_assoc($result)) {
            $lista[] = $row["id_campo"];
            $lista[] = $row["valor"];
        }

        return $lista; //ToDo: return errors
    }

    public function getPatientImmu ($pid) {
        //Because DB is bad designed, we have to transform the varchar to date
        //then get the last immunization for each 'virus'
        //and return a str with the venezuelan format
        
        $query = "SELECT id_campo, DATE_FORMAT( valor2,  '%d/%m/%Y' ) AS value
          FROM (
            SELECT id_campo, MAX( STR_TO_DATE( valor,  '%d/%m/%Y' ) ) AS valor2
            FROM care_historia_datos_historia_clinica
            WHERE id_paciente =  '".$pid."'
            AND ficha =  'inmunizaciones'
            AND id_campo !=  'otras_especificacion'
            GROUP BY id_campo
          ) AS auxtable";
        $result = mysql_query($query) or die(mysql_error());
        
        //$result = mysql_fetch_array($result);
        //echo $result;

        $lista = array();
        while ($row = mysql_fetch_assoc($result)) {
            $lista[$row["id_campo"]] = $row["value"];
        }
        
        //if there's a case with 'otras' we need the especification
        if ($lista["otras"]!="00/00/0000") {
          $query = "SELECT id_campo, valor
            FROM care_historia_datos_historia_clinica
            WHERE id_campo = 'otras_especificacion'
              AND id_paciente =  '".$pid."'";
          $result = mysql_query($query) or die(mysql_error());
          
          while ($row = mysql_fetch_assoc($result)) {
            $lista[$row["id_campo"]] = $lista[$row["id_campo"]] . $row["valor"];
          }
        }

        return $lista;
    }
}
?>
